package com.wangxiang.shop.member.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.shop.member.dao.MemberDao;
import com.wangxiang.shop.member.dao.ReceiptDao;
import com.wangxiang.shop.member.entity.CartEntity;
import com.wangxiang.shop.member.entity.MemberEntity;
import com.wangxiang.shop.member.entity.SkuEntity;
import com.wangxiang.shop.member.service.MemberService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.shop.member.dao.ShopCartDao;
import com.wangxiang.shop.member.entity.ShopCartEntity;
import com.wangxiang.shop.member.service.ShopCartService;
import org.springframework.web.bind.annotation.RequestMapping;

import static jdk.nashorn.internal.objects.NativeMath.round;


@Service("shopCartService")
public class ShopCartServiceImpl extends ServiceImpl<ShopCartDao, ShopCartEntity> implements ShopCartService {
    @Autowired
    private ShopCartDao shopCartDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private ReceiptDao receiptDao;




    @Override
    public int add(HashMap<String, String> map, List<SkuEntity> skuEntityList) {
//        System.out.println(map);
        System.out.println(skuEntityList);
        ShopCartEntity shopCartEntity = new ShopCartEntity();
        String style = map.get("style");
        String size =  map.get("size");

        shopCartEntity.setSpuId(Integer.parseInt(map.get("spuId")));
        shopCartEntity.setNum(Integer.valueOf(map.get("num")));
        shopCartEntity.setUserId(Integer.parseInt(map.get("userId")));
        Date date = new Date();
        shopCartEntity.setTime(date);
        if (map.get("openId")!=null){
            shopCartEntity.setOpenId(map.get("openId"));
        }

        if (map.get("size")==null&map.get("style")==null){
            System.out.println(skuEntityList);
            if (skuEntityList.size()!=0){
                shopCartEntity.setSkuId(skuEntityList.get(0).getId());
            }

        }
        //都不为空
        if (style!=null&size!=null){
            if (skuEntityList.size()!=0){
                for (SkuEntity skuEntity : skuEntityList) {
                    if (style.equals(skuEntity.getStyle())&size.equals(skuEntity.getSize())){
                        System.out.println("都不为空");
                        shopCartEntity.setSkuId(skuEntity.getId());
                        shopCartEntity.setPrice(skuEntity.getPrice());
                    }
                }
            }

        }
        //style为空，size不为空
        if (style==null&size!=null){
            if (skuEntityList.size()!=0){
                for (SkuEntity skuEntity : skuEntityList) {
                    if (style==skuEntity.getStyle()&size.equals(skuEntity.getSize())){
                        System.out.println("style为空，size不为空");
                        shopCartEntity.setSkuId(skuEntity.getId());
                        shopCartEntity.setPrice(skuEntity.getPrice());
                    }
                }
            }

        }
        //style不为空，size为空
        if (style!=null&size==null){
            if (skuEntityList.size()!=0){
                for (SkuEntity skuEntity : skuEntityList) {
                    if (style.equals(skuEntity.getStyle())&size==skuEntity.getSize()){
                        System.out.println("style不为空，size为空");
                        shopCartEntity.setSkuId(skuEntity.getId());
                        shopCartEntity.setPrice(skuEntity.getPrice());
                    }
                }
            }

        }



//        System.out.println("购物车实体"+shopCartEntity);
        if (shopCartEntity.getSpuId() != 0){
            int insert = shopCartDao.insert(shopCartEntity);
            return insert;
        }

        return 0;
    }

    @Override
    public List<ShopCartEntity> list(int userId) {
        List<ShopCartEntity> cartListByUserId = shopCartDao.getCartListByUserId(userId);
        List<ShopCartEntity> shopCartEntities = new ArrayList<>();
        for (ShopCartEntity shopCartEntity : cartListByUserId) {
            if (shopCartEntity.getStatus()==0){
                shopCartEntities.add(shopCartEntity);
            }
        }
        return shopCartEntities;
    }

    @Override
    public ShopCartEntity info(int cartId) {
        ShopCartEntity shopCartEntity = shopCartDao.selectById(cartId);
        return shopCartEntity;
    }

    @Override
    public void updateStatus(int cartId) {
        shopCartDao.updateStatus(cartId , 1);
    }

    @Override
    public PageEntity page(PageEntity page) {

        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<ShopCartEntity> shopCartEntities = shopCartDao.selectList(null);
//        List<Integer> spuIdList = new ArrayList<>();
        for (ShopCartEntity shopCartEntity : shopCartEntities) {
            MemberEntity member = memberDao.selectById(shopCartEntity.getUserId());
            shopCartEntity.setUserName(member.getName());
//            spuIdList.add()
        }
        PageInfo<ShopCartEntity> PageInfo = new PageInfo<>(shopCartEntities);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(PageInfo);
        return generate;
    }

    @Override
    public List<HashMap<String, Integer>> updateWare(List<Integer> cartId) {
        List<HashMap<String, Integer>> list = new ArrayList<>();
        for (Integer integer : cartId) {
            ShopCartEntity shopCartEntity = shopCartDao.selectById(integer);
            HashMap<String, Integer> map = new HashMap<>();
            map.put("skuId",shopCartEntity.getSkuId());
            map.put("num",shopCartEntity.getNum());
            list.add(map);
        }
        return list;
    }

    @Override
    public List<CartEntity> getCartEntity(List<ShopCartEntity> entities) {
        ArrayList<CartEntity> cartEntities = new ArrayList<>();
        for (ShopCartEntity info : entities) {
            CartEntity cartEntity = new CartEntity();
            ShopCartEntity shopCartEntity = shopCartDao.selectById(info.getCartId());
            cartEntity.setCartId(shopCartEntity.getCartId());
            cartEntity.setSpuId(shopCartEntity.getSpuId());
            cartEntity.setSkuId(shopCartEntity.getSkuId());
            cartEntity.setUserId(shopCartEntity.getUserId());
            cartEntity.setUnitPrice(shopCartEntity.getPrice()); //单价
            double ap = shopCartEntity.getPrice()*shopCartEntity.getNum();
            DecimalFormat df = new DecimalFormat("0.00");
            String format = df.format(ap);
            double aDouble = Double.valueOf(format);
            cartEntity.setAllPrice(aDouble);
            cartEntity.setNum(shopCartEntity.getNum());
            cartEntity.setStatus(shopCartEntity.getStatus());
            TimeUtils timeUtils = new TimeUtils();
            String time = timeUtils.timeChange(shopCartEntity.getTime());
            cartEntity.setTime(time);
            cartEntities.add(cartEntity);
            MemberEntity member = memberDao.selectById(shopCartEntity.getUserId());
            cartEntity.setUserName(member.getName());

        }
        return cartEntities;
    }


    @Override
    public PageEntity search(HashMap<String, Object> map) {
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int pageNum = Integer.valueOf(map.get("pageNum").toString());
        String name = map.get("search").toString();
        //查询member表获取userId
        List<MemberEntity> memberEntities = memberDao.selectList(null);
        //以防止管理员带空格查询
        String replace = map.get("search").toString().replace(" ", "");
        ArrayList<Integer> integers = new ArrayList<>();
        memberEntities.forEach((item)->{
            if (name.equals(item.getName())){
                integers.add(item.getUserId());
            }
        });
        //得到UserId查询，userId下的购物车信息
        if (integers.size()==0){
            return null;
        }else {
            List<ShopCartEntity> list = shopCartDao.getCartListByUserId(integers.get(0));
            for (ShopCartEntity shopCartEntity : list) {
//                System.out.println(shopCartEntity);
            }
            PageHelper.startPage(pageSize, pageNum);
            PageInfo<ShopCartEntity> shopCartEntityPageInfo = new PageInfo<>(list);
            PageGenerate pageGenerate = new PageGenerate();
            PageEntity generate = pageGenerate.generate(shopCartEntityPageInfo);
            return generate;

        }




    }

    @Override
    public List<CartEntity> getCartEntityBySearch(List<ShopCartEntity> entities , String name) {
        ArrayList<CartEntity> cartEntities = new ArrayList<>();
        for (ShopCartEntity shopCartEntity : entities) {
//            System.out.println("实体"+shopCartEntity);
            CartEntity cartEntity = new CartEntity();
            cartEntity.setCartId(shopCartEntity.getCartId());
            cartEntity.setSpuId(shopCartEntity.getSpuId());
            cartEntity.setSkuId(shopCartEntity.getSkuId());
            cartEntity.setUserId(shopCartEntity.getUserId());
            cartEntity.setUnitPrice(shopCartEntity.getPrice()); //单价
            cartEntity.setAllPrice(shopCartEntity.getPrice()*shopCartEntity.getNum());
            cartEntity.setNum(shopCartEntity.getNum());
            cartEntity.setStatus(shopCartEntity.getStatus());
            TimeUtils timeUtils = new TimeUtils();
            String time = timeUtils.timeChange(shopCartEntity.getTime());
            cartEntity.setTime(time);
            cartEntities.add(cartEntity);
//            System.out.println("会员"+member);
            cartEntity.setUserName(name);
            cartEntities.add(cartEntity);
        }
//        System.out.println(cartEntities);
        return cartEntities;
    }


}